<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:pic:add']"
        >
          新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete()"
          v-hasPermi="['system:pic:remove']"
        >
          删除
        </el-button>
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="dataList"
      border
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="轮播图" align="center" min-width="150">
        <template slot-scope="scope">
          <el-image
            :src="baseUrl + scope.row.pic"
            :preview-src-list="[baseUrl + scope.row.pic]"
            style="width: 100px; height: 70px"
          />
        </template>
      </el-table-column>
      <el-table-column label="跳转地址" align="center" prop="jumpUrl" min-width="200" />
      <el-table-column label="排序" align="center" prop="sort" width="180" />
      <el-table-column label="操作" align="center" width="180">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:pic:edit']"
          >
            修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:pic:remove']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      :title="form.id ? '新增轮播图' : '修改轮播图'"
      :visible.sync="open"
      width="50%"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="轮播图" prop="pic">
          <Image-upload v-model="form.pic" :limit="1" :isShowTip="false" />
        </el-form-item>
        <el-form-item label="跳转地址" prop="jumpUrl">
          <el-input v-model="form.jumpUrl" placeholder="请输入跳转地址" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入排序" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPic, getPic, delPic, addPic, updatePic } from "@/api/system/pic";

export default {
  name: "Pic",
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,
      loading: false, // 遮罩层
      ids: [], // 选中数组
      single: true, // 非单个禁用
      multiple: true, // 非多个禁用
      dataList: [], // 首页轮播图列表格数据
      title: "", // 弹出层标题
      open: false, // 是否显示弹出层
      // 查询参数
      queryParams: {
        pic: null,
      },
      form: {}, // 表单参数
      rules: {
        pic: [{ required: true, message: "轮播图不能为空", trigger: "blur" }],
        jumpUrl: [{ required: true, message: "跳转地址不能为空", trigger: "blur" }],
        sort: [
          { required: true, message: "排序不能为空", trigger: "blur" },
          { pattern: /^[1-9]\d*$/, message: "请输入正整数", trigger: "blur" },
        ],
      }, // 表单校验
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询首页轮播图列列表 */
    getList() {
      this.loading = true;
      listPic(this.queryParams)
        .then((res) => {
          this.dataList = res.data;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        pic: null,
        jumpUrl: null,
        sort: null,
      };
      this.resetForm("form");
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.getList();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      if (this.dataList.length >= 4) {
        this.$message.warning("最多只能添加4个轮播图");
        return;
      }
      this.reset();
      this.open = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getPic(id).then((res) => {
        this.form = res.data;
        this.open = true;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id) {
            updatePic(this.form).then((res) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPic(this.form).then((res) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row ? [row.id] : this.ids;
      this.$modal
        .confirm('确认删除轮播图列编号为"' + ids + '"的数据项？')
        .then(() => {
          delPic(ids).then(() => {
            this.getList();
            this.$modal.msgSuccess("删除成功");
          });
        })
        .catch(() => {});
    },
  },
};
</script>
